import re
from bs4 import BeautifulSoup
import requests


def get_forms(soup):
    """从页面中提取ASP.NET表单隐藏字段"""
    try:
        viewstate = soup.find("input", attrs={"name": "__VIEWSTATE"})["value"] if soup.find("input", attrs={
            "name": "__VIEWSTATE"}) else ""
        viewstategenerator = soup.find("input", attrs={"name": "__VIEWSTATEGENERATOR"})["value"] if soup.find("input",
                                                                                                              attrs={
                                                                                                                  "name": "__VIEWSTATEGENERATOR"}) else ""
        eventvalidation = soup.find("input", attrs={"name": "__EVENTVALIDATION"})["value"] if soup.find("input", attrs={
            "name": "__EVENTVALIDATION"}) else ""

        return {
            "__EVENTTARGET": "lkbt_next",
            "__VIEWSTATE": viewstate,
            "__VIEWSTATEGENERATOR": viewstategenerator,
            "__EVENTVALIDATION": eventvalidation
        }
    except Exception as e:
        print(f"提取表单数据出错: {e}")
        return None


def get_next_page():
    """获取分页数据"""
    session = requests.Session()  # 使用session保持会话
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
    }
    session.headers.update(headers)

    url = "https://www.cnzj5u.com/list_block.aspx?stid=187&rptid=8"

    # 获取第一页
    res = requests.get(url)
    soup = BeautifulSoup(res.text, "html.parser")

    # 提取第一页数据
    title_table = soup.find("table", attrs={"id": "GridView1"})
    if title_table:
        h2_list = title_table.find_all("h2")
        for h2 in h2_list:
            print(h2.text)

    # 获取后续页面
    for page in range(2, 100):  # 获取第2到第9页
        print(f"\n--- 第{page}页 ---")

        # 提取表单数据
        form_data = get_forms(soup)
        if not form_data:
            print(f"无法获取第{page}页的表单数据")
            break

        # 发送POST请求获取下一页
        try:
            res = requests.post(url, data=form_data)
            soup = BeautifulSoup(res.text, "html.parser")

            # 提取数据
            title_table = soup.find("table", attrs={"id": "GridView1"})
            if title_table:
                h2_list = title_table.find_all("h2")
                for h2 in h2_list:
                    print(h2.text)
            else:
                print(f"第{page}页未找到数据表格")
                break

        except Exception as e:
            print(f"获取第{page}页时出错: {e}")
            break


# 执行爬取
get_next_page()
